package br.ufrgs.inf.sissw.app;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ZoomControls;
import br.ufrgs.inf.sissw.app.models.InfItemizedOverlay;
import br.ufrgs.inf.sissw.app.models.InfOverlayItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/*
 * Salas de aula = -30.068690, -51.120636
 * Anfi grande = -30.068797, -51.120367
 * 67	= -30.068699, -51.121156
 * Ildo = -30.068945, -51.120625
 */
public class MapsActivity extends MapActivity {

	private MapController mapController;
	private MapView mapView;
	private GeoPoint p;
	private ZoomControls zoomControls;
	int zoomLevel = 19;
	private List<Overlay> mapOverlays;
	private Drawable drawable;
	private InfItemizedOverlay itemizedOverlay;

	private OnClickListener goHomeListener = new OnClickListener() {

		public void onClick(View v) {
			goHome(MapsActivity.this);
		}
	};

	public void goHome(Context context) {
		final Intent intent = new Intent(context, InfAppActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.maps_activity);

		ImageButton homeButton = (ImageButton) findViewById(R.id.feature_home_button);
		homeButton.setOnClickListener(goHomeListener);

		TextView activityName = (TextView) findViewById(R.id.feature_name);
		activityName.setText(R.string.maps);
		setMapController();
		setMapOverlays();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// populateMap();
		centerMap();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private void setMapController() {

		try {
			mapView = (MapView) findViewById(R.id.mapView);
			mapView.setSatellite(true);
			mapController = mapView.getController();
			mapController.setZoom(zoomLevel);

			zoomControls = (ZoomControls) findViewById(R.id.zoomControls);

			zoomControls.setOnZoomInClickListener(new OnClickListener() {

				public void onClick(View v) {
					mapController.setZoom(++zoomLevel);

				}
			});

			zoomControls.setOnZoomOutClickListener(new OnClickListener() {

				public void onClick(View v) {
					mapController.setZoom(--zoomLevel);

				}
			});

		} catch (Exception e) {
			System.out
					.println(">>> OndeAbastecerActivity.setMapController() - "
							+ e.getMessage());
			e.printStackTrace();
		}
	}

	private void centerMap() {

		mapController.animateTo(new GeoPoint((int) (-30.068870 * 1000000),
				(int) (-51.120545 * 1000000)));
		mapController.setZoom(zoomLevel);

	}

	private void setMapOverlays() {
		mapOverlays = mapView.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.simple_marker);
		itemizedOverlay = new InfItemizedOverlay(drawable, this);

		// 67
		GeoPoint point = new GeoPoint(-30068723, -51121136);
		InfOverlayItem overlayitem = new InfOverlayItem(point, "", "",
				"43413 / 67");

		// Ildo
		GeoPoint point2 = new GeoPoint(-30068992, -51120565);
		InfOverlayItem overlayitem2 = new InfOverlayItem(point2, "", "",
				"43412");

		// Salas de aula
		GeoPoint point3 = new GeoPoint(-30068532, -51120538);
		InfOverlayItem overlayitem3 = new InfOverlayItem(point3, "", "",
				"43425");

		// Laboratórios
		GeoPoint point4 = new GeoPoint(-30068732, -51120317);
		InfOverlayItem overlayitem4 = new InfOverlayItem(point4, "", "",
				"43424");

		itemizedOverlay.addOverlay(overlayitem);
		itemizedOverlay.addOverlay(overlayitem2);
		itemizedOverlay.addOverlay(overlayitem3);
		itemizedOverlay.addOverlay(overlayitem4);
		mapOverlays.add(itemizedOverlay);
	}
}
